package cn.itcast.exercise

object WordCount2 {
  def main(args: Array[String]): Unit = {
    var list =List("you name is tom name","jack java")
    val words = list.flatMap(_.split(" "))
    val wordandOne = words.map(word => (word, 1))
    val groupbywords = wordandOne.groupBy(t => t._1)
    val method1 = groupbywords.map(t => (t._1, t._2.length))
  //  val method2 = groupbywords.mapValues(_.length)
    print(method1)
    //上面的代码可以合并为一行
    val result = list.flatMap(t => t.split(" ")).map(t => (t, 1)).groupBy(t => t._1).map(t => (t._1, t._2.length))
    println("一行结果："+result)
  }
}
